package com.xiaoxin.yixinai.userinfo;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.xiaoxin.yixinai._frame.base.BaseRepository;

@Repository
public interface UserInfoRepository extends BaseRepository<UserInfo> {

	/**
	 * 查询一个公司的管理员， 因为老数据中，tb_company_id 的 admin_id 中没有存公司的管理员ID，
	 * 所以为了兼容老数据，在查询公司的管理员时，用这个方法。 在公司开户时，会创建一个管理员，可以保证查询这个公司下的第1个人员就是管理员。
	 * 
	 * @param companyId
	 * @param isDelete
	 * @return
	 */
	UserInfo findTop1ByCompanyIdAndIsDeleteOrderByUserIdAsc(Integer companyId, Integer isDelete);

	/**
	 * 通过邀请码查询用户（前提是邀请码唯一）
	 * 
	 * @param myInviterCode
	 *            邀请码(唯一)
	 * @param isDelete
	 * @return
	 */
	UserInfo findByMyInviterCodeAndIsDelete(String myInviterCode, Integer isDelete);

	List<UserInfo> findByIsDeleteAndCompanyIdIn(Integer isDelete, Collection<Integer> companyIds);

	UserInfo findByLoginNameAndIsDelete(String loginName, Integer is_delete);

	/**
	 * 查询一个公司拥有的员工个数
	 * 
	 * @param companyId
	 *            公司ID
	 * @return
	 */
	@Query("select count(1) as num from UserInfo where isDelete = 0 and companyId = :companyId")
	int findUserNumByCompnayId(@Param("companyId") Integer companyId);

	@Query("select count(userId) as co from UserInfo where isDelete = 0 and companyId = :companyId and groupId = :groupId")
	int getUserNumsByGroupId(@Param("companyId") Integer companyId, @Param("groupId") int groupId);

	@Query("select max(t.orderIndex) as orderIndex from UserInfo t where isDelete =0 and companyId=:companyId")
	Integer getMaxOrderIndex(@Param("companyId") Integer companyId);

	@Query("select max(t.orderIndex) from UserInfo t where companyId=:companyId and groupId=:groupId order by orderIndex desc")
	Integer getMaxOrderIndex(@Param("companyId") Integer companyId, @Param("groupId") Integer groupId);

	@Query("from UserInfo where isDelete = 0 and isVirtual = 1 and linkId in (:userIdList)")
	List<UserInfo> findAllByLinkId(@Param("userIdList") List<Integer> userIdList);

	@Modifying
	@Query("update UserInfo set groupId = :groupId , officeTel= :officeTel, orderIndex=:orderIndex, job = :job  where userId = :userId")
	void updateGroupIdAndOfficeTelAndOrderIndexAndJobByUserId(Integer groupId, String officeTel, Integer orderIndex,
			String job, Integer linkId);

	@Query("from UserInfo where isDelete=0  and userId in (:userIds) order by orderIndex")
	List<UserInfo> findUsersByIds(@Param("userIds") Collection<Integer> userIds);

	@Query("from UserInfo where isDelete=0  and groupId in (:groupIds) order by orderIndex")
	List<UserInfo> findUsersByGoupdIds(@Param("groupIds") List<Integer> groupIds);

	@Query("from UserInfo where isDelete=0  and companyId=:companyId and groupId =:groupId and userName LIKE CONCAT('%',:userName,'%') order by orderIndex")
	List<UserInfo> findUsersByGoupdIdAndCompanyIdAndUserName(@Param("groupId") Integer groupId,
			@Param("companyId") Integer companyId, @Param("userName") String userName);

	@Query("from UserInfo where isDelete=0  and companyId=:companyId  order by orderIndex")
	List<UserInfo> findUserListByCompanyId(@Param("companyId") Integer companyId);

	@Query("from UserInfo where isDelete=0 and groupId = :groupId and companyId=:companyId  order by orderIndex")
	List<UserInfo> findUserListByGroupIdAndCompanyId(@Param("groupId") Integer groupId,
			@Param("companyId") Integer companyId);

	@Modifying
	@Query("update UserInfo set isDelete=1,groupId = null,lastUpdateTime=:lastUpdateTime where userId in (:userIds)")
	void deleteUserByIds(@Param("userIds") List<Integer> userIds, @Param("lastUpdateTime") Date lastUpdateTime);

	/**
	 * 通过公司ID集合查询用户集合
	 * 
	 * @param companyIds
	 *            公司ID集合
	 * @param isDelete
	 * @return
	 */
	List<UserInfo> findByIsDelete(Integer isDelete);

	List<UserInfo> findByCompanyIdAndIsDelete(Integer companyId, int isDelete);

	@Query("select o from UserInfo o where companyId =:companyId and seatPhone !=null and isDelete = 0")
	List<UserInfo> findSeats(@Param("companyId") Integer companyId);

	@Query("select o from UserInfo o where userId in :userIds and companyId =:companyId")
	List<UserInfo> findByUserIds(@Param("userIds") List<Integer> userIds, @Param("companyId") Integer companyId);
}
